package connect;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import util.ByteBuffer;
import action.MessageAction;
import app.App;

public class MyIoHandler extends IoHandlerAdapter
{

	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		cause.printStackTrace();
	}

	public void messageReceived(IoSession session, Object message)
			throws Exception {
		if(message instanceof ByteBuffer)
		{
			ByteBuffer data = (ByteBuffer) message;
			App.getInstance().pushAction(new MessageAction(data,session.hashCode()));
			
		}else
		{
			System.err.println("收到消息解析错误");
		}
	}

	public void messageSent(IoSession session, Object message) throws Exception {

	}

	public void sessionClosed(IoSession session) throws Exception
	{
		System.err.println("sessionClosed");
		App.getInstance().connects.remove(session.hashCode());
		App.getInstance().removePlayer(session);
		
	}

	public void sessionOpened(IoSession session) throws Exception {
		App.getInstance().connects.put(session.hashCode(), session);
	}
	
	
	@Override
	public void sessionCreated(IoSession session) throws Exception
	{
		System.err.println("sessionCreated");
	}

}
